package cn.tedu.ttl.front.account.controller;

import cn.tedu.ttl.front.account.pojo.param.UserRegisterParam;
import cn.tedu.ttl.front.account.pojo.param.UserUpdateInfoParam;
import cn.tedu.ttl.front.account.pojo.vo.UserSimpleInfoVO;
import cn.tedu.ttl.front.account.service.IUserService;
import cn.tedu.ttl.common.consts.web.HttpConsts;
import cn.tedu.ttl.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.ttl.common.validation.account.UserRules;
import cn.tedu.ttl.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;

/**
 * 处理用户相关请求的控制器类
 *
 * @author java@tedu.cn
 * @version 3.0
 */
@Slf4j
@RestController
@RequestMapping("/users")
@Validated
@Api(tags = "1. 用户管理")
public class UserController implements HttpConsts {

    @Autowired
    private IUserService userService;

    public UserController() {
        log.info("创建控制器对象：UserController");
    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    @ApiOperationSupport(order = 10)
    public JsonResult login(@Validated UserRegisterParam userLoginInfoParam) {
        log.debug("开始处理【用户注册】的请求，参数：{}", userLoginInfoParam);
        userService.register(userLoginInfoParam);
        return JsonResult.ok();
    }

    @PostMapping("/info/update")
    @ApiOperation("修改基本信息")
    @ApiOperationSupport(order = 300)
    public JsonResult updateInfo(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal,
                                 @Valid UserUpdateInfoParam userUpdateInfoParam) {
        log.debug("开始处理【修改基本信息】的请求，当事人：{}，新基本信息：{}", currentPrincipal, userUpdateInfoParam);
        userService.updateInfo(currentPrincipal, userUpdateInfoParam);
        return JsonResult.ok();
    }

    @PostMapping("/password/update")
    @ApiOperation("修改密码")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oldPassword", value = "原密码", required = true, paramType = "query"),
            @ApiImplicitParam(name = "newPassword", value = "新密码", required = true, paramType = "query")
    })
    public JsonResult updatePassword(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal,
                                     @Pattern(regexp = UserRules.PATTERN_PASSWORD, message = UserRules.MESSAGE_PASSWORD_PATTERN) String oldPassword,
                                     @Pattern(regexp = UserRules.PATTERN_PASSWORD, message = UserRules.MESSAGE_PASSWORD_PATTERN) String newPassword) {
        log.debug("开始处理【修改密码】的请求，当事人：{}，原密码：{}，新密码：{}", currentPrincipal, oldPassword, newPassword);
        userService.updatePassword(currentPrincipal, oldPassword, newPassword);
        return JsonResult.ok();
    }

    @PostMapping("/avatar/update")
    @ApiOperation("修改头像")
    @ApiOperationSupport(order = 320)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "avatar", value = "新头像的路径", required = true, paramType = "query")
    })
    public JsonResult updateAvatar(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal,
                                   @NotBlank(message = "请提交新头像的路径") String avatar) {
        log.debug("开始处理【修改头像】的请求，当事人：{}，新头像：{}", currentPrincipal, avatar);
        userService.updateAvatar(currentPrincipal, avatar);
        return JsonResult.ok();
    }

    @PostMapping("/phone/update")
    @ApiOperation("修改手机号码")
    @ApiOperationSupport(order = 330)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号码", required = true, paramType = "query")
    })
    public JsonResult updatePhone(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal,
                                  @Pattern(regexp = UserRules.PATTERN_PHONE, message = UserRules.MESSAGE_PHONE_PATTERN) String phone) {
        log.debug("开始处理【修改手机号码】的请求，当事人：{}，新手机号码：{}", currentPrincipal, phone);
        userService.updatePhone(currentPrincipal, phone);
        return JsonResult.ok();
    }

    @PostMapping("/email/update")
    @ApiOperation("修改电子邮箱")
    @ApiOperationSupport(order = 340)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "email", value = "电子邮箱", required = true, paramType = "query")
    })
    public JsonResult updateEmail(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal,
                                  @Pattern(regexp = UserRules.PATTERN_EMAIL, message = UserRules.MESSAGE_EMAIL_PATTERN) String email) {
        log.debug("开始处理【修改电子邮箱】的请求，当事人：{}，新手机号码：{}", currentPrincipal, email);
        userService.updateEmail(currentPrincipal, email);
        return JsonResult.ok();
    }

    @GetMapping("/info")
    @ApiOperation("查询当前用户基本信息")
    @ApiOperationSupport(order = 400)
    public JsonResult getSelfSimpleInfo(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal) {
        log.debug("开始处理【查询当前用户基本信息】的请求，当事人：{}", currentPrincipal);
        UserSimpleInfoVO selfSimpleInfo = userService.getSelfSimpleInfo(currentPrincipal);
        return JsonResult.ok(selfSimpleInfo);
    }

}
